<template>
  <div class="app-container">
    <el-row>
      <el-col :span="16">
        <el-row :gutter="20">
          <el-col :span="24" :xs="24">
            <el-form
              v-show="showSearch"
              ref="queryForm"
              :model="queryParams"
              :inline="true"
              label-width="68px"
              label-position="left"
            >
              <el-form-item label="关键字" prop="keyword">
                <el-input
                  v-model="queryParams.keyword"
                  placeholder="请输入关键字"
                  clearable
                  size="small"
                  style="width: 240px"
                  @keyup.enter.native="handleQuery"
                />
              </el-form-item>
              <el-form-item label="关键字类别" prop="wordType" label-width="100px">
                <el-select v-model="queryParams.wordType" size="small" style="width: 240px" placeholder="请选择" clearable>
                  <el-option
                    v-for="(item,index) in wordTypeList"
                    :key="index"
                    :label="item"
                    :value="item"
                  />
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="handleQuery"
                >搜索</el-button>
              </el-form-item>
            </el-form>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  v-hasPermission="['knowledge:keyword:add']"
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd"
                >新增</el-button>
              </el-col>
              <el-col :span="1.5">
                <el-button
                  v-hasPermission="['knowledge:keyword:delete']"
                  type="danger"
                  plain
                  icon="el-icon-delete"
                  size="mini"
                  @click="handleDelete"
                >删除</el-button>
              </el-col>
              <el-col :span="3">
                <el-button
                  v-hasPermission="['knowledge:keyword:updateCache']"
                  type="success"
                  plain
                  icon="el-icon-refresh"
                  size="mini"
                  @click="handleUpdateCacheKeyword"
                >更新缓存</el-button>
              </el-col>
            </el-row>
            <el-table v-loading="loading" :data="keywordList" style="width: 100%" empty-text="暂无数据" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" />
              <el-table-column label="序号" type="index" align="center" />
              <el-table-column prop="keyword" label="关键字" align="center" show-overflow-tooltip />
              <el-table-column prop="wordType" label="类别" align="center" show-overflow-tooltip />
              <el-table-column prop="wordMean" label="类别意思" align="center" show-overflow-tooltip />
              <el-table-column
                label="操作"
                align="center"
                width="300"
              >
                <template v-if="scope.row.id !== 1" slot-scope="scope">
                  <el-button
                    v-hasPermission="['knowledge:keyword:update']"
                    size="mini"
                    type="primary"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                  >修改</el-button>
                  <el-button
                    v-hasPermission="['knowledge:keyword:delete']"
                    size="mini"
                    type="danger"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <el-pagination
          :page-size.sync="queryParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :page-sizes="[10, 20, 30, 40]"
          :current-page.sync="queryParams.pageNum"
          @current-change="getList"
          @size-change="getList"
        />
      </el-col>
      <el-col :span="8">
        <el-row>
          <TagChart :url="tagUrl" />
        </el-row>
        <el-row>
          <HistogramChart :title="histogramTitle" :unit="histogramUnit" :url="fullUrl" />
        </el-row>
        <el-row>
          <TypeChart :url="fullUrl" />
        </el-row>
      </el-col>
    </el-row>
    <!-- 添加或修改分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="关键字" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请输入关键字" style="width: 240px" />
        </el-form-item>
        <el-form-item label="关键字类别" prop="wordType">
          <el-select v-model="form.wordType" style="width: 240px" placeholder="请选择" clearable>
            <el-option
              v-for="(item,index) in wordTypeList"
              :key="index"
              :label="item"
              :value="item"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import TagChart from '@/views/common/TagChart'
import TypeChart from '@/views/common/TypeChart'
import HistogramChart from '@/views/common/HistogramChart'
import { Loading, Message } from 'element-ui'
import {
  addKeyword,
  delKeyword,
  getKeyword,
  listKeyword,
  listWordType,
  updateCache,
  updateKeyword
} from '@/api/knowledge/keywords'
export default {
  name: 'Keyword',
  components: { HistogramChart, TypeChart, TagChart },
  data() {
    return {
      tagUrl: '/knowledge/keyword/randomTag',
      fullUrl: '/knowledge/keyword/fullTag',
      histogramTitle: '柱状图(关键字)',
      histogramUnit: '个',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined,
        wordType: undefined
      },
      loading: true,
      // 显示搜索条件
      showSearch: true,
      total: 0,
      keywordList: [],
      wordTypeList: [],
      ids: [],
      wordTypes: [],
      form: {
        id: '',
        keyword: '',
        wordType: '',
        wordMean: ''
      },
      open: false,
      // 表单校验
      rules: {
      },
      title: ''
    }
  },
  created() {
  },
  mounted() {
    listWordType().then(response => {
      this.wordTypeList = response
    })
    this.getList()
  },
  methods: {
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.wordTypes = []
      selection.forEach(item => {
        if (!this.wordTypes.includes(item.wordType)) {
          this.wordTypes.push(item.wordType)
        }
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleAdd() {
      this.title = '新增关键字'
      this.open = true
      this.reset()
    },
    handleDelete(row) {
      let ids = []
      if (row.id) {
        ids.push(row.id)
      } else {
        ids = this.ids
      }
      let wordTypes = []
      if (row.wordType) {
        wordTypes.push(row.wordType)
      } else {
        wordTypes = this.wordTypes
      }
      const data = {
        ids: ids,
        wordTypes: wordTypes
      }
      this.$modal.confirm('确认删除？').then(function() {
        return delKeyword(data)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    getList() {
      this.loading = true
      listKeyword(this.queryParams).then(response => {
        this.keywordList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getKeyword(id).then(response => {
        this.form = response
        this.open = true
        this.title = '修改关键字'
      })
    },
    handleUpdateCacheKeyword() {
      const loadingInstance = Loading.service({
        lock: true,
        text: '正在更新ing',
        spinner: 'el-icon-loading',
        background: 'rgba(0,0,0,0.7)'
      })
      updateCache().then(response => {
        this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
          setTimeout(function() {
            loadingInstance.close()
            Message.success('操作成功')
          }, 500)
        })
      }).catch(() => {
        loadingInstance.close()
      })
    },
    reset() {
      this.form = {
        id: null,
        keyword: undefined,
        wordMean: undefined,
        wordType: undefined
      }
      this.resetForm('form')
    },
    submitForm() {
      if (this.form.id === null) {
        addKeyword(this.form).then(response => {
          this.$modal.msgSuccess('新增成功')
          this.open = false
          this.getList()
        })
      } else {
        updateKeyword(this.form).then(response => {
          this.$modal.msgSuccess('修改成功')
          this.open = false
          this.getList()
        })
      }
    },
    cancel() {
      this.reset()
      this.open = false
    }
  }
}
</script>

<style scoped>

</style>
